<?php
namespace App\Controller;

use App\Model\DynamicModel;
use lib\View;
use App\Model\UserModel;
use App\Model\CommentModel;
use App\Model\FriendsModel;
use App\Model\GalleryModel;
use App\Model\PicturesModel;
use App\Model\MailModel;
use App\Model\RecoveryModel;
use lib\ArrTree;

class indexController
{


   public function index(){
    if (!isset($_SESSION['user'])) {
      return new View('login');
    }
    else{
    $user=  $_SESSION['user']  ;
   $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
   $edit = new UserModel();
   $data = $edit->get(['username' => $user]);
   $_SESSION['pic'] = $data[0]['pic']; 
   $contents  = $this->viewDynamic();

        //获取网页页顶信息框的数据
        $mails = new MailModel();
        $emails = $mails->get(['user_id'=>$user_id]);

        $count=$mails->count_email($emails);  
        $_SESSION['count'] = $count;
        
        $friends_Data= $this->friends_right_list();
        $userData =  $friends_Data['userData'];
        $fData = $friends_Data['fData'];
   return new View('index',['user'=>$user,'user_id'=>$user_id,'data'=>$data[0],'userData'=>$userData,'fData'=>$fData,'contents'=>$contents,'count'=>$count]);
     }
   }
   



   public function addFriends(){
        if (!isset($_SESSION['user'])) {
      return new View('login');
    }
    else{
    $user=  $_SESSION['user']  ;
    $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
    $user_pic = isset($_SESSION['pic']) ? $_SESSION['pic']  : '';
    $count = isset($_SESSION['count']) ? $_SESSION['count'] : '';

    $edit = new UserModel();
    $data = $edit->get(['username' => $user]);

        $friends_Data= $this->friends_right_list();
        $userData =  $friends_Data['userData'];
        $fData = $friends_Data['fData'];

        return new View('addFriends',['user'=>$user,'userpic'=>$user_pic,"data"=>$data[0],'userData'=>$userData,'fData'=>$fData,'count'=>$count]);
      }
   }



   public function doAddFriends(){
        $fid=$_GET['fid'];
        $f=new FriendsModel();
        $data['uid']=$_SESSION['user_id'];
        $data['fid']=$fid;
        $res=$f->save($data);
        if($res){
            success('添加好友成功!','addFriends');
                }
         else{
            error('添加好友失败!');
              }
        }



    public function friends_right_list(){
        $fids=[];
        $f=new FriendsModel();
        $frData=$f->get(['uid'=>$_SESSION['user_id']]);
        foreach($frData as $v){
            $fids[]=$v['fid'];
        }
        $get_user=new UserModel();
        $userData=$get_user->get();

        //去掉登录用户的信息
        foreach($userData as $k=>$v){
            if($v['id']==$_SESSION['user_id']){
                unset($userData[$k]);
            }
            //去掉好友信息
            if(in_array($v['id'],$fids)){
                unset($userData[$k]);
            }
        }
        //返回好友信息
        $fData=$get_user->join(['friends'=>'fid'],['friends.uid'=>$_SESSION['user_id']]);
        $friends_Data['fData']=$fData;
        $friends_Data['userData'] = $userData;
        return $friends_Data;
    }
   

    public function communicate()
    {
        if (!isset($_SESSION['user'])) {
      return new View('login');
    }
    else{
    $user=  $_SESSION['user']  ;
     $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
     $edit = new UserModel();
    $data = $edit->get(['username' => $user]);
    $_SESSION['pic'] = $data[0]['pic']; 

        //获取网页页顶信息框的数据
        $mails = new MailModel();
        $emails = $mails->get(['user_id'=>$user_id]);

        $count=$mails->count_email($emails);  
        $_SESSION['count'] = $count;
        
        $friends_Data= $this->friends_right_list();
        $userData =  $friends_Data['userData'];
        $fData = $friends_Data['fData'];

        return new View('communicate',['user'=>$user,"data"=>$data[0],'userData'=>$userData,'fData'=>$fData,'count'=>$count]);
     }
   }
      
    

    public function profile(){
      if (!isset($_SESSION['user'])) {
      return new View('login');
    }
        else{
    $user= $_SESSION['user']  ;
    $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
    $uid = isset($_GET['uid']) ? $_GET['uid'] : '';
    $count = isset($_SESSION['count']) ? $_SESSION['count'] : '';
    if (!empty($user)) {
      if (!empty($uid)) {
      $edit = new UserModel();
      $data = $edit->get(['id' => $uid]);
      }
    else {
   		$edit = new UserModel();
   		$data = $edit->get(['username' => $user]);
    }
   		$username = isset($data[0]['username']) ? $data[0]['username']  : '';
   		$email = isset($data[0]['email']) ? $data[0]['email']  : '';
   		$About = isset($data[0]['About']) ? $data[0]['About']  : '';
   		$Phone = isset($data[0]['Phone']) ? $data[0]['Phone']  : '';
   		$Hobbies = isset($data[0]['Hobbies']) ? $data[0]['Hobbies']  : '';
   		$gender = isset($data[0]['gender']) ? $data[0]['gender']  : '';
   		$birthday = isset($data[0]['birthday']) ? $data[0]['birthday']  : '';
      $pic = isset($data[0]['pic']) ? $data[0]['pic']  : '';
       $friends_Data= $this->friends_right_list();
       $userData =  $friends_Data['userData'];
      $fData = $friends_Data['fData'];
    }

   	return new View('profile',['user'=>$user,'username'=>$username,'email'=>$email,'Phone'=>$Phone,'About'=>$About,'Hobbies'=>$Hobbies,'gender'=>$gender,'birthday'=>$birthday,'pic'=>$pic,'count'=>$count,'data'=>$data[0],'fData'=>$fData,'userData'=>$userData]);
   }
   }




   public static function createAlbum(){
    
    $user =$_SESSION['user'];
    $user_id=$_SESSION['user_id'];
    $files = isset($_FILES['imgs']) ? $_FILES['imgs'] : '';
    $data1['users_id'] =$user_id;
    $data1['create_at'] = date('Y-m-d H:i:s',time());
    $edit= new GalleryModel();
    $edit->save($data1);
      $album_id = $edit->get(['create_at'=>$data1['create_at']])[0]['id'];
    $de = new GalleryController();
        $user =$_SESSION['user'];
    $user_id=$_SESSION['user_id'];
    if (!empty($user)) 
    {
    $pics=$de->uploadHeadPicture($files);
     $save = new PicturesModel();
    foreach ($pics as $pic) {
        $data['album_id'] =$album_id;
        $data['path'] =$pic;
        $data['upload_at'] = date('Y-m-d H:i:s',time());
        $save->save($data);
    }
    success('上传成功','index');
     }
}
  


   public function add_blog(){
   	return new View('add_blog');
   }



   public function blog_detail(){
   	return new View('blog_detail');
   }



   public function blog_list(){
   	return new View('blog_list');
   }



   public function lock(){
          if (!isset($_SESSION['user'])) {
      return new View('login');
    }
    else{
      $user=  $_SESSION['user']  ;
     $pic = isset($_SESSION['pic']) ? $_SESSION['pic'] : '' ;
      $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
     $users =  new UserModel();
     $data['id'] =$user_id;
     $data['is_online'] =0;
     $users->save($data);
     unset($_SESSION['user']);
       return new View('lock',['pic'=>$pic,'user'=>$user]);
     }
   }




    public function edit(){
          if (!isset($_SESSION['user'])) {
      return new View('login');
    } else{
   		  $user=  $_SESSION['user']  ;
   		$user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
      $count = isset($_SESSION['count']) ? $_SESSION['count'] : '';
   		if (!empty($user)) {
   		$edit = new UserModel();
   		$data = $edit->get(['username' => $user]);
   		$email = isset($data[0]['email']) ? $data[0]['email']  : '';
   		$username = isset($data[0]['username']) ? $data[0]['username']  : '';
   		$phone = isset($data[0]['Phone']) ? $data[0]['Phone']  : 0;
   		$hobbies = isset($data[0]['Hobbies']) ? $data[0]['Hobbies']  : '';
   		$gender = isset($data[0]['gender']) ? $data[0]['gender']  : '';
   		$birthday = isset($data[0]['birthday']) ? $data[0]['birthday']  : date('Y-m-d H:i:s',time());
   		$about = isset($data[0]['About']) ? $data[0]['About']  : '';
      $pic = isset($data[0]['pic']) ? $data[0]['pic']  : '';

       $friends_Data= $this->friends_right_list();
        $userData =  $friends_Data['userData'];
        $fData = $friends_Data['fData'];
   		}
   	return new View('edit',['user'=>$user,'username'=>$username,'email'=>$email,'phone'=>$phone,'about'=>$about,'hobbies'=>$hobbies,'gender'=>$gender,'birthday'=>$birthday,'pic'=>$pic,'count'=>$count,'data'=>$data[0],'fData'=>$fData,'userData'=>$userData]);
   }
   }



    public function do_edit(){
   		$user =$_SESSION['user'];
   		$user_id=$_SESSION['user_id'];
      $files = isset($_FILES['imgs']) ? $_FILES['imgs'] : '';

   		if (!empty($user)) {
        //图片上传部分
        $de = new GalleryController();
        $d=$de->uploadHeadPicture($files)[0];
        //基本资料上传
   		$username = $_POST['username'];
   		$email = $_POST['email'];
   		$phone = $_POST['phone'];
   		$hobbies = $_POST['hobbies'];
   		$gender = $_POST['gender'];
   		$birthday = $_POST['birthday'];
   		$about = $_POST['about'];

        $data['pic']= $d;
   			$data['id'] =$user_id;
   			$data['username'] =$username;
   			$data['email'] =$email;
   			$data['Phone'] = $phone;
   			$data['Hobbies'] = $hobbies;
   			$data['gender'] = $gender;
   			$data['birthday'] = $birthday;
   			$data['About'] = $about;

   			$save= new UserModel();
   			$save->save($data);

   			$_SESSION['user'] = $username;
   			success('发送成功','index');

   		}   error('修改失败');
   }



     public function dynamic(){
		
		$user =$_GET['user'];
		$user_id =$_GET['user_id'];
		$edit = new UserModel();
    $UserData = $edit->get(['username' => $user]);
    $headPicture = $UserData[0]['pic'];
		if (!empty($user)) {
			$content = $_POST['contents'] ;
			if (!empty($content)) {
    $files = isset($_FILES['photos']) ? $_FILES['photos'] : '';
    $de = new GalleryController();
    $pics=$de->uploadHeadPicture($files);

    $pics=implode(' #,# ',$pics);
    $d = new DynamicModel();
        $data['pics'] = $pics;
			  $data['users_id'] =$user_id;
  			$data['content'] =$content;
  			$data['likenum'] =0;
		    $data['upload_at']= time();
			$d->save($data);

			success('发送成功','index');
			}	
			else {	
				$_SESSION['error'] = '留言不能为空';
				$errorBag=$_SESSION['error'];
				unset($_SESSION['error']);
			 error('发送失败,'.$errorBag);
			}
		 }	
	  }
   

public static function viewDynamic(){
  

  //先获取所有动态，通过用户ID筛选
  $dynamic = new DynamicModel();
  $d= $dynamic->get();

  //将照片文件转换成数组格式
   

  //取出好友信息
  $user_id=$_SESSION['user_id'];
  $fs= new FriendsModel();
  $friends = $fs->get(['uid'=>$user_id]);
  $users[] = $user_id;

 foreach ($friends as $key1 => $value1) {
  $users[]=$friends[$key1]['fid']; 
}
 
  $p=[];

  //取出好友的动态信息
  foreach ($d as $key2 => $value2) {
  if (in_array($value2['users_id'], $users)) {
       $p[]=$value2;
      }
  }
  //构造动态信息
  foreach ($p as $key3 => $value3) {
    
    //将图片信息转成数组
    $photos=explode('#,#',$value3['pics']); 
    $p[$key3]['pics']=$photos;

    //获取评论信息
    $comment = new CommentModel();
    $user= new UserModel();
    $comments= $comment->get(['dynamic_id'=>$value3['id']]);
    $tree=new ArrTree();
    $comments=$tree->tree($comments,'id');
    //获取回复用户名的信息
    foreach ($comments as $key4 => $value4) {    
    if($value4['rid'] !=0 ){
           $reapeated_user_id=$comment->get(['id'=>$value4['rid']])[0]['users_id'];
           $reapeated_user_name= $user->get(['id'=>$reapeated_user_id])[0]['username'];
            $comments[$key4]['reapeated_user'] = $reapeated_user_name;
        }
    } 

    //构造评论信息
    foreach ($comments as $key5 => $value5) {
    $users_id=$value5['users_id'];
    $username = $user->get(['id'=>$users_id])[0]['username'];
    $pic = $user->get(['id'=>$users_id])[0]['pic'];
    $comments[$key5]['userData']['username'] = $username;
    $comments[$key5]['userData']['pic'] = $pic;
  }

     
    $p[$key3]['comments'] = $comments;

    //加上用户信息
    $u=$user->get(['id'=>$value3['users_id']])[0];
    $p[$key3]['userinfo']=$u;
  }
  return $p;
}



  public static function delete_dynamic()
  {
    $data['id'] = $_GET['dynamic_id'];
    $edit = new DynamicModel();
    $edit ->deletes($data);
    success('删除成功','index');
    } 


  public static function comment(){

    $rid = $_POST['rid'];
    $user =$_POST['username'];
    $example = $_POST['send_comment'];
    $dynamic_id = $_POST['dynamic_id'];
    $edit = new UserModel();
    $UserData = $edit->get(['username' => $user]);
    $user_id = $UserData[0]['id'];
    $Comment = new CommentModel();

    $data['rid'] = $rid;
    $data['users_id'] =$user_id;
    $data['content'] =$example;
    $data['dynamic_id'] =$dynamic_id;
    $data['send_at']= time();
    $Comment->save($data);
    success('发送成功','index');
    }

    

 public static function delete_comment()
  {
    $data['id'] = $_GET['comment_id'];
    unset($_GET['comment_id']);
    $edit = new CommentModel();
    $edit ->deletes($data);
    success('删除成功','index');
    } 

}
